package com.adun.scheduler;

import lombok.Data;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author ZDF
 * @date 2025/8/8
 */
@Data
public abstract class AbstractSchedulerJob<T> implements ISchedulerJob<T>{

    /**
     * 任务名称,约定，必须使用BeanName作为任务名称,仅英文
     */
    private String jobName;

    private String jobDescription;

    private String jobCron;

    private Integer scheduledThreadPoolCoreSize;

    private String scheduledThreadPrefix;

    private String jobBeanName;

    private String jobClassName;

    private Class<T> jobClassType;


    public AbstractSchedulerJob() {
        Type superClass = this.getClass().getGenericSuperclass();
        if (superClass instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) superClass;
            Type[] types = parameterizedType.getActualTypeArguments();
            this.jobClassType = (Class<T>) types[0];
            this.jobClassName = this.jobClassType.getName();
            String jobName = getJobName();
            this.setJobName(StringUtils.hasText(jobName) ? jobName : getBeanName());
            this.setJobBeanName(getJobBeanName());
        }
    }

    private String getBeanName(){
        String simpleName = this.jobClassType.getSimpleName();
        return StringUtils.uncapitalize(simpleName);
    }

    @Override
    public Integer getScheduledThreadPoolCoreSize() {
        return 3;
    }

    @Override
    public String getScheduledThreadPrefix() {
        return "loc-";
    }

    @Override
    public abstract void initJobParams();

    @Override
    public abstract void execute();

    @PostConstruct
    public void autoInitParams(){
        this.initJobParams();
    }
}
